package com.hygj.controller;

import com.hygj.pojo.Auth;

import com.hygj.service.AuthService;

import com.hygj.util.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping
public class AuthController {

    @Autowired
	private  AuthService authService;
   
    
    /**
     * 菜单添加
     *
     * @return
     */
    @RequestMapping(value="insertAuth" ,method = RequestMethod.POST)
    public String insertAuth(Auth auth) {
        authService.insert(auth);
        return "redirect:getAuthAll";
    }
    

    /**
     * 查询全部菜单信息(列表用)
     *
     * @param map
     * @param request
     * @return
     */
    @RequestMapping("getAuthAll")
    public String getAuthAll(@ModelAttribute("search") Auth auth, Map<String, Object> map,
                              HttpServletRequest request) {
        Map<String, Object> params = new HashMap<String, Object>();
        // 第一步：判断长度，是否为空
        if (StringUtils.isNotBlank(auth.getAuthName())) {
            params.put("authName", "%" + auth.getAuthName() + "%");
        }
        PageUtil<Auth> page = authService.page(request, 10, params);
        map.put("page", page);
        return "home/admin/auth/auth_show";
    }


    /**
     * 删除，多项删除 @RequestParam("inform")引入 选择框 inform
     * ===><input type="checkbox" name="inform">
     * @return
     */
    @RequestMapping(value="delete_auth",method = RequestMethod.POST)
    public String delete_auth(@RequestParam("inform") Integer[] ids){
        authService.delete(ids);
        return "redirect:getAuthAll";
    }


    /**
     * 回显菜单信息(根据‘authId’)
     *
     * @param authId
     * @param modelMap
     * @return
     */
    @RequestMapping("getAuthById")
    public String getAuthById(Integer authId, ModelMap modelMap) {
        Auth auths = authService.getById("getById", authId);
        modelMap.addAttribute("auths", auths);
        return "home/admin/auth/auth_update";
    }

    /**
     * 菜单信息修改
     *
     * @param auth
     * @return
     */
    @RequestMapping("updateAuth")
    public String updateAuth(Auth auth) {
        authService.update("update", auth);
        return "redirect:getAuthAll";
    }



}
